#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: set_seed_flag.py
@time: 2017/12/27 22:02
"""
import sys

sys.path.append('..')
sys.path.append('../..')
from common import util

from common.mongo import MongDb
from logger import Logger

MONGO_DB_SOURCE = {
    'host': '172.16.215.2',
    'port': 40042,
    'db': 'company_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('copy_offline_zhejiang_list.log').get_logger()

source_db = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                   MONGO_DB_SOURCE['username'],
                   MONGO_DB_SOURCE['password'], log=log)


def main():
    count = 0

    source_table = 'offline_zhejiang_list'
    target_table = 'new_zhejiang_company_list'
    result_list = []
    for item in source_db.traverse_batch_field(source_table, {}, ['_id']):
        _id = item.get('_id')

        if '*' in _id:
            continue

        if '+' in _id:
            continue

        count += 1
        _in_time = util.get_now_time()

        result_list.append({
            '_id': _id,
            '_in_time': _in_time,
            'crawl_status': 0,
        })
        if len(result_list) >= 500:
            source_db.insert_batch_data(target_table, result_list)
            del result_list[:]
        if count % 1000 == 0:
            log.info("当前完成进度: count = {}".format(count))

    if len(result_list) > 0:
        source_db.insert_batch_data(target_table, result_list)
        del result_list[:]

    log.info("数据复制完成...")


if __name__ == '__main__':
    main()
